package cn.tju.yanbo.commons;

import java.util.Calendar;

import cn.tju.yanbo.R;
import cn.tju.yanbo.commons.anim.AnimationController;
import cn.tju.yanbo.commons.anim.AppAnimationUtils;
import cn.tju.yanbo.utils.AppConfig;
import cn.tju.yanbo.utils.AppUtils;
import android.graphics.Point;
import android.graphics.Rect;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.widget.AbsoluteLayout.LayoutParams;
import android.widget.ImageView;

public class ImageViewTouchListener extends CalendarGestureListener{

    private ImageView queryImageView = null;
    private ImageView [] viewList = new ImageView[AppConfig.TOTAL_YIJI_IMAGE_VIEW_COUNT];
    private Rect [] boundsList = new Rect[AppConfig.TOTAL_YIJI_IMAGE_VIEW_COUNT];
    
    public ImageViewTouchListener(BaseActivity activity) {
        super(activity);
        
        queryImageView = (ImageView)activity.findViewById(R.id.yiji_query);
        viewList[0] = (ImageView)activity.findViewById(R.id.yiji_nacai);
        viewList[1] = (ImageView)activity.findViewById(R.id.yiji_jisi);
        viewList[2] = (ImageView)activity.findViewById(R.id.yiji_anchuang);
        viewList[3] = (ImageView)activity.findViewById(R.id.yiji_chuxing);
        viewList[4] = (ImageView)activity.findViewById(R.id.yiji_dongtu);
    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        
        boundsList[0] = new Rect(viewList[0].getLeft(), viewList[0].getTop(), viewList[0].getRight(), viewList[0].getBottom());
        boundsList[1] = new Rect(viewList[1].getLeft(), viewList[1].getTop(), viewList[1].getRight(), viewList[1].getBottom());
        boundsList[2] = new Rect(viewList[2].getLeft(), viewList[2].getTop(), viewList[2].getRight(), viewList[2].getBottom());
        boundsList[3] = new Rect(viewList[3].getLeft(), viewList[3].getTop(), viewList[3].getRight(), viewList[3].getBottom());
        boundsList[4] = new Rect(viewList[4].getLeft(), viewList[4].getTop(), viewList[4].getRight(), viewList[4].getBottom());
        
        int index = AppUtils.touchOnWhichBounds(new Point((int)e.getX(), (int)e.getY()), boundsList);
        if(index!=-1) {
            String tag = AppUtils.toString(viewList[index].getTag());
            String drawableKey = AppUtils.getDrawableKey(tag);
            
            queryImageView.setImageDrawable(activity.CACHED_PICTURES.get(drawableKey));
            queryImageView.setTag(drawableKey);
            activity.update(controller.time());
            return true;
        }
        return false;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        boundsList[0] = new Rect(viewList[0].getLeft(), viewList[0].getTop(), viewList[0].getRight(), viewList[0].getBottom());
        boundsList[1] = new Rect(viewList[1].getLeft(), viewList[1].getTop(), viewList[1].getRight(), viewList[1].getBottom());
        boundsList[2] = new Rect(viewList[2].getLeft(), viewList[2].getTop(), viewList[2].getRight(), viewList[2].getBottom());
        boundsList[3] = new Rect(viewList[3].getLeft(), viewList[3].getTop(), viewList[3].getRight(), viewList[3].getBottom());
        boundsList[4] = new Rect(viewList[4].getLeft(), viewList[4].getTop(), viewList[4].getRight(), viewList[4].getBottom());
        
        int len4judge = AppUtils.SCREEN_METRICS.widthPixels/16;
        int index = AppUtils.touchOnWhichBounds(new Point((int)e1.getX(), (int)e1.getY()), boundsList);
        if(Math.abs(e1.getX()-e2.getX())>=len4judge && index!=-1) {
            switchImageView(viewList, e2.getX()>e1.getX());
        } else {
            Point p1 = new Point((int)e1.getX(), (int)e1.getY());
            if(p1.y>AppConfig.DATE_COMPASS_CENTROID.y && AppUtils.isPointInCompass(p1, activity.center(), activity.compassRadius())) {
                return revolveDateView(e1, e2, velocityX, velocityY);
            } else {
                return switchActivity(e1, e2, velocityX, velocityY);
            }
        }
        return false;
    }

    private void switchImageView(ImageView [] views, boolean right) {
        if(right) {
            for(int i=0; i<viewList.length; i++){
                Log.i("gui", "bf:"+i+"-left="+viewList[i].getLeft()+",top="+viewList[i].getTop()+",right="+viewList[i].getRight()+",bottom="+viewList[i].getBottom());
            }
            this.translate(views[views.length-1], views[0]);
            for(int i=views.length-2; i>=0; i--) {
                Animation anim = AppAnimationUtils.loadTranslateAnimation(views[i], views[i+1], AppConfig.ANIMATION_DURATION);
                views[i].startAnimation(anim);
            }
            for(int i=0; i<viewList.length; i++){
                Log.i("gui", "aft:"+i+"-left="+viewList[i].getLeft()+",top="+viewList[i].getTop()+",right="+viewList[i].getRight()+",bottom="+viewList[i].getBottom());
            }
            ImageView tmpView = viewList[views.length-1];
            for(int i=views.length-1; i>0; i--) {
                views[i] = views[i-1];
            }
            views[0] = tmpView;
            for(int i=0; i<viewList.length; i++){
                Log.i("gui", "aft111:"+i+"-left="+viewList[i].getLeft()+",top="+viewList[i].getTop()+",right="+viewList[i].getRight()+",bottom="+viewList[i].getBottom());
            }
        } else {
            this.translate(views[0], views[views.length-1]);
            for(int i=1; i<views.length; i++) {
                Animation anim = AppAnimationUtils.loadTranslateAnimation(views[i], views[i-1], AppConfig.ANIMATION_DURATION);
                viewList[i].startAnimation(anim);
            }
            ImageView tmpView = viewList[0];
            for(int i=0; i<views.length-1; i++) {
                views[i] = views[i+1];
            }
            views[views.length-1] = tmpView;
        }
    }
    
    private void translate(View view1, View view2) {
//        int deltaX = view2.getLeft()-view1.getLeft();
//        int deltaY = view2.getTop()-view1.getTop();
//        int width = view1.getRight()-view1.getLeft();
//        int height = view1.getBottom()-view1.getTop();
////        view1.offsetLeftAndRight(deltaX);
////        view1.offsetTopAndBottom(deltaY);
        Animation anim = AppAnimationUtils.loadTranslateAnimation(view1, view2, AppConfig.ANIMATION_ZERO_DURATION);
        view1.startAnimation(anim);
    }
}
